/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include "InquireHandler.h"

#include <map>
#include <thread>
#include <fstream>
#include <iostream>
#include <functional>
#include "BackintConst.h"
#include "common/File.h"
#include "common/MpString.h"
#include "common/JsonHelper.h"

using namespace backint;

namespace {
    const std::string INQUIRE_SUCCESS = "#BACKUP";
    const std::string INQUIRE_ERROR = "#ERROR";
    const std::string INQUIRE_NOTFOUND = "#NOTFOUND";
}

uint32_t InquireHandler::ExecInquire(const StuParam &inParam, const PlugParams &plugParam)
{
    LOGGUARD("");
    GetInquireParam(inParam, plugParam);
    StuFileInfo stFileInfo;
    if (GetBackupList(inParam.m_inputFile, stFileInfo) != SUCCESS) {
        ERRLOG("Failed to get backup info, jobId=%s, subJobId=%s.", m_jobId.c_str(), m_subJobId.c_str());
        return FAILED;
    }

    const size_t fileSplitCount = 2;
    for (const auto &it : stFileInfo.fileData) {
        std::vector<std::string> vesConents;
        Module::CMpString::StrSplit(vesConents, it.m_filePath, ' ');
        if (vesConents.size() == 1) {
            // 查找m_dataPath下的.anf文件
            std::vector<std::string> ves;
            FindAnfSuffixFiles(m_dataPath, ves);
            for (const auto &it : ves) {
                INFOLOG("The anf file:%s", it.c_str());
                std::cout << INQUIRE_SUCCESS << " " << GetFileName(it) << std::endl;
            }
        } else if (vesConents.size() == fileSplitCount) {
            // 查找m_dataPath下的filePath文件
            std::string filePath = vesConents[1];
            INFOLOG("The intput file:%s", vesConents[1].c_str());
            std::string bkfilePath = m_dataPath + PATH_SEP_CHAR + GetFileName(it.m_filePath);
            if (!Module::CFile::FileExist(bkfilePath)) {
                std::cout << INQUIRE_NOTFOUND << " " << GetFileName(filePath) << std::endl;
            } else {
                std::cout << INQUIRE_SUCCESS << " " << GetFileName(filePath) << std::endl;
            }
        }
    }
    return SUCCESS;
}

void InquireHandler::GetInquireParam(const StuParam &inParam, const PlugParams &plugParam)
{
    m_jobId = plugParam.m_jobId;
    m_subJobId = plugParam.m_subJobId;
    m_cachePath = plugParam.m_cachePath;
    m_dataPath = plugParam.m_dataPath;
    m_outputFile = inParam.m_outFile;
    m_backupID = inParam.m_dtBackupId;
}